#include <list.h>

int list_add(char *en, char *cn, NODE *head)
{
    NODE *new = NULL;

    new = (NODE *)malloc(sizeof(NODE));
    ERRP(NULL == new, return 0, 2, "malloc node failed!\n");
    new->cn = cn;
    new->en = en;

    new->next = head;
    new->prev = head->prev;
    head->prev->next = new;
    head->prev = new;

    return 1;
}

int list_like_find(char *key, int num, char **word, NODE *head)
{
    int count = 0, len;
    NODE *tail = NULL;

    len = strlen(key);

    for (tail = head->next; tail != head; tail = tail->next)
    {
        if (!strncmp(key, tail->en, len))
        {
            word[count++] = tail->en;
            if (count == num)
                break;
        }
    }

    return count;
}

void list_destroy(NODE *head)
{
    NODE *tail = head->next, *save = NULL;

    while (tail != head)
    {
         save = tail->next;
         free(tail->cn);
         free(tail->en);
         free(tail);
         tail = save;
    }
    head->next = head;
    head->prev = head;
}

